//
//  OAuthModel.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/19.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

/*
["access_token": 2.004IwNPGcsclSBac5f2f56eaEgIIEB, "remind_in": 157679999, "uid": 5721763767, "expires_in": 157679999]

*/
/// 用户账户key
private let GXLWeiboUserCountKey = "com.bai.user"


class OAuthModel: NSObject{
    /// 用户账户单利
    static let shareUserAccount = OAuthModel()
    
    
    /// 构造函数

private  override  init() {
        super.init()
    
      loaduserAccount()
    
    }
    
    var isLogin:Bool{
        
        
        return access_token != nil
    }
    
    

    /// 用于调用access_token，接口获取授权后的的access token
    var access_token:String?
    
    var remind_in:Int = 0
    /// 当前授权用户的UID
    var  uid:Int = 0
    /// access——token的生命周期
    var expires_in: NSTimeInterval = 0 {
        didSet {
            // 计算过期日期
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    /// 过期日期
    var expiresDate: NSDate?
    /// 用户昵称
    var name:String?
    /// 用户大图 180*180
    var avatar_large:String?
    
   
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
       
    }
    
 override   var description: String {
     //模型转字典
    
    let keys = ["access_token","uid","expires_in","expiresDate","name","avatar_large"]
    
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
//    required init?(coder aDecoder: NSCoder) {
//        
//        access_token = aDecoder.decodeObjectForKey("access_token") as? String
//        
//        name = aDecoder.decodeObjectForKey("name") as? String
//        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
//        
//        uid = aDecoder.decodeIntegerForKey("uid")
//        
//        remind_in = aDecoder.decodeIntegerForKey("remind_in")
//        
//        expires_in = aDecoder.decodeDoubleForKey("expires_in")
//        
//        // 解档过期时间
//        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
//        
//        
//        
//       
//    }
    
//    //归档
//    func encodeWithCoder(aCoder: NSCoder) {
//        
//        aCoder.encodeObject(access_token, forKey: "access_token")
//        aCoder.encodeObject(name, forKey: "name")
//        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
//        
//        aCoder.encodeInteger(remind_in, forKey: "remind_in")
//        
//        aCoder.encodeInteger(uid, forKey: "uid")
//        
//        
//        
//        aCoder.encodeDouble(expires_in, forKey: "expires_in")
//        
//        // 将过期时间也要归档
//        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
//        
//    }
    
    

    
    
 
    
 
 
}

    

  
    


// MARK: - 加载和保存用户账户
extension OAuthModel {
    
   /// 使用字典 更新 保存用户数据
    
    func upDataWithDict(dict: [String: AnyObject]){
        
        
        setValuesForKeysWithDictionary(dict)
        
        saveUserAccount()
        
        
    }
    
    
    
    
/// 将但前用户的账户保存在偏好设置中
  private  func saveUserAccount(){
        
        //1模型转字典
        
        let keys = ["access_token","uid","expires_in","expiresDate","name","avatar_large"]
        
        let dict = dictionaryWithValuesForKeys(keys)
        
        //将字典写入到偏好设置
        NSUserDefaults.standardUserDefaults().setObject(dict, forKey: GXLWeiboUserCountKey)
        
        print(NSHomeDirectory())
        
        
    }
    
/// 加载用户信息
    private func loaduserAccount(){
        
        /// 1从用户偏好加载
        guard   let dict = NSUserDefaults.standardUserDefaults().objectForKey(GXLWeiboUserCountKey) as? [String: AnyObject] else {
            
            return
        }
        
         /// 字典转模型
        setValuesForKeysWithDictionary(dict)
        
        //判断是否过期，过期则设置为nil
        
        if isExpired {
            print("token 已经过期")
            access_token = nil
        } else {
            print("token 正常")
        }
        
        
    }
    
    /// 判断是否过期
    private var isExpired: Bool {
        // 将过期日期和当前系统日起进行比较
        // 举个栗子
        // 2016-01-01
        // 2016-02-15
        // 2016-12-30
        return expiresDate?.compare(NSDate()) == NSComparisonResult.OrderedAscending
    }
    
    
}

